package com.gaga.console.base.config;

import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import com.gaga.common.path.ApiPath;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册Sa-Token的路由拦截器
        registry.addInterceptor(new SaRouteInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(notLogin());

        // 注册注解拦截器
        registry.addInterceptor(new SaAnnotationInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(notLogin());
    }

    /**
     * 放行的url
     *
     * @return
     */
    public static List<String> notLogin() {

        List<String> allows = new ArrayList<>();
        allows.add("/" + ApiPath.ROOT + "/auth/**");
        allows.add("/" + ApiPath.ROOT + "/bv/test/**");
        allows.add("/" + ApiPath.ROOT + "/sys/config/configKey/**");
        //放行所有
//        allows.add("/" + ApiPath.ROOT + "/**");

        //swagger资源放行
        allows.add("/v2/**");
        allows.add("/v3/**");
        allows.add("/*.html");
        allows.add("/swagger-ui.html/**");
        allows.add("/swagger-ui/**");
        allows.add("/doc.html/**");
        allows.add("/api-docs-ext/**");
        allows.add("/swagger-resources/**");
        allows.add("/webjars/**");
        allows.add("/favicon.ico");
        allows.add("/error");

        return allows;
    }

}
